package example.com.terry.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;


// 操作 jdbc 的工具类
public class JDBCUtils {
    private static Properties prop = new Properties();
    //注册驱动的工具类
    private static String url = null;
    private static String userName = null;
    private static String password = null;
    private static String jdbcDriver = null;
    private static Connection conn = null;
    private static PreparedStatement ps = null;
    private static Logger log = Logger.getLogger("dev");

    public static ResultSet initQuery(String sql) {
        log.info("------------JDBC进入初始化------------");
        ResultSet resultSet = null;
        InputStream in = JDBCUtils.class.getResourceAsStream("/prop.properties");
        //调用properies文件链接数据库
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //配置JDBC
        url = prop.getProperty("url");
        userName = prop.getProperty("username");
        password = prop.getProperty("password");
        jdbcDriver = prop.getProperty("driver");
        try {
            // 加载驱动
            Class.forName(jdbcDriver);
            log.info("------------JDBC开始获取连接------------");
            // 获取连接
            conn = DriverManager.getConnection(url, userName, password);
            // 创建会话
            ps = conn.prepareStatement(sql);
            // 执行sql
            resultSet = ps.executeQuery();
            log.info("------------JDBC获取mysql数据------------");
        } catch (ClassNotFoundException e) {
            destroy();
            //主动抛出异常提示错误问题
            log.info("----------JDBC驱动加载失败----------");

        } catch (SQLException e) {
            destroy();
            //主动抛出异常提示错误问题
            log.info("----------sql语句执行失败----------");
        } catch (NullPointerException e) {
            log.info("----------配置文件prop.properties读取发生异常----------");
        }
        return resultSet;
    }

    // 用?传参的方式执行sql
    public static boolean encodingInit(String sql, String[] values, int length) {
        int i = 0;
        InputStream in = JDBCUtils.class.getResourceAsStream("/prop.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        url = prop.getProperty("url");
        userName = prop.getProperty("username");

        password = prop.getProperty("password");
        jdbcDriver = prop.getProperty("driver");
        try {
            // 加载驱动
            Class.forName(jdbcDriver);
            // 获取连接
            conn = DriverManager.getConnection(url, userName, password);
            // 创建会话
            ps = conn.prepareStatement(sql);
            for (int j = 0; j < length; j++) {
                ps.setString(j + 1, values[j]);
                System.out.println();
            }
            // 执行sql
            i = ps.executeUpdate();
        } catch (ClassNotFoundException e) {
            destroy();
            //主动抛出异常提示错误问题
            log.info("----------JDBC驱动加载失败----------");

        } catch (SQLException e) {
            destroy();
            //主动抛出异常提示错误问题
            log.info("----------sql语句执行失败----------");
        } catch (NullPointerException e) {
            log.info("----------配置文件prop.properties读取发生异常----------");
        }
        return i > 0 ? true : false;
    }

    public static boolean init(String sql) {
        int i = 0;
        InputStream in = JDBCUtils.class.getResourceAsStream("/prop.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        url = prop.getProperty("url");
        userName = prop.getProperty("username");

        password = prop.getProperty("password");
        jdbcDriver = prop.getProperty("driver");
        try {
            // 加载驱动
            Class.forName(jdbcDriver);
            // 获取连接
            conn = DriverManager.getConnection(url, userName, password);
            // 创建会话
            ps = conn.prepareStatement(sql);
            // 执行sql
            i = ps.executeUpdate();
        } catch (ClassNotFoundException e) {
            //主动抛出异常提示错误问题
            destroy();
            log.info("----------JDBC驱动加载失败----------");

        } catch (SQLException e) {
            //主动抛出异常提示错误问题
            destroy();

            log.info("----------sql语句执行失败----------");
        } catch (NullPointerException e) {
            log.info("----------配置文件prop.properties读取发生异常----------");
        }
        return i > 0 ? true : false;

    }

    public static void destroy() {
        try {
            if (null != ps) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            log.info("----------关闭数据库异常----------");
        }
    }
}
